package com.woniu.mangercontrol;
import com.woniu.HisHospitalizationApi.HisBedApi;
import com.woniu.HisHospitalizationApi.HisRoomApi;
import com.woniu.api.HisDepartmentAPI;
import com.woniu.api.HisOfficeAPI;
import com.woniu.api.HisSchedulingAPI;
import com.woniu.api.HisStaffAPI;
import com.woniu.dto.*;
import com.woniu.exception.DIYException;
import com.woniu.hismanger.api.MangerApi;
import com.woniu.hismanger.param.*;
import com.woniu.hismanger.param.HisBedAddParam;
import com.woniu.param.*;
import com.woniu.util.CopyBeanUtil;
import com.woniu.util.Result;
import com.woniu.utils.OssUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping("/manager")
@Slf4j
public class ManagerControl implements MangerApi {
    @Resource
    private HisBedApi hisBedApi;
    //病房
    @Resource
    private HisRoomApi hisRoomApi;
    //员工
    @Resource
    private HisStaffAPI hisStaffAPI;

    //科室
    @Resource
    private HisOfficeAPI hisOfficeAPI;

    @Resource
    private HisSchedulingAPI hisSchedulingAPI;

    @Override
    public Result DepartmentDisplay(HisOfficeParam officeParam) {
        QueryOfficeParam queryOfficeParam=new QueryOfficeParam();
        if (officeParam.getQueryParam()==null){

            Result<QueryOfficeDto> queryOfficeDtoResult = hisOfficeAPI.queryOffice(queryOfficeParam);
            return queryOfficeDtoResult;
        } else {
            Integer officeId = officeParam.getQueryParam().getOfficeId();
            queryOfficeParam.setOfficeId(officeId);
            Result<QueryOfficeDto> queryOfficeDtoResult = hisOfficeAPI.queryOffice(queryOfficeParam);
            return queryOfficeDtoResult;
        }

    }

    @Override
    public Result CreatOffice(HisCreateOfficeParam hisCreateOfficeParam) {
        CreateOfficeParam createOfficeParam = CopyBeanUtil.copyBean(hisCreateOfficeParam, CreateOfficeParam.class);
        Result<OfficeDto> office = hisOfficeAPI.createOffice(createOfficeParam);
        return office;
    }

    @Override
    public Result DepartmentDetail(HisQueryStationParam queryStationParam) {
        HisStationWhereParam stationWhereParam = queryStationParam.getStationWhereParam();
        PageParam pageParam = queryStationParam.getPageParam();
        QueryCriteriaStaffParam queryCriteriaStaffParam = CopyBeanUtil.copyBean(stationWhereParam, QueryCriteriaStaffParam.class);
        queryCriteriaStaffParam.setStaffIdentity(0);
        QueryStaffParam queryStaffParam = new QueryStaffParam();
        queryStaffParam.setQueryCriteriaStaffParam(queryCriteriaStaffParam);
        queryStaffParam.setPageParam(pageParam);
        Result<QueryStaffDto> queryStaffDtoResult = hisStaffAPI.queryStaff(queryStaffParam);
        PageDTO<StaffDto> staffDtoList = queryStaffDtoResult.getData().getStaffDtoList();
        return Result.bulid(staffDtoList);
    }

    @Override
    public Result staffDisplay(HisQueryStaffParam hisqueryStaffParam) {
        PageParam pageParam = new PageParam();
        QueryCriteriaStaffParam queryCriteriaStaffParam = CopyBeanUtil.copyBean(hisqueryStaffParam.getQueryCriteriaStaffParam(), QueryCriteriaStaffParam.class);

        QueryStaffParam queryStaffParam = new QueryStaffParam();
        queryStaffParam.setPageParam(pageParam);
        queryStaffParam.setQueryCriteriaStaffParam(queryCriteriaStaffParam);
        Result<QueryStaffDto> queryStaffDtoResult = hisStaffAPI.queryStaff(queryStaffParam);

        return queryStaffDtoResult;
    }

    @Override
    public Result AddStaff(HisStaffParam hisStaffParam) {
          hisStaffParam.setStaffImage("https://wnxy-61.oss-cn-chengdu.aliyuncs.com/1633a649-bf1e-4393-bfc3-bbdec8ed4a92.exe");
        CreateStaffParam createStaffParam = CopyBeanUtil.copyBean(hisStaffParam, CreateStaffParam.class);

        hisStaffAPI.createStaff(createStaffParam);
        return null;
    }

    @Override
    public Result upLode( MultipartFile File, String name) {
        System.out.println(File+"++++");
        String fileType = UUID.randomUUID().toString()+ File.getOriginalFilename()
                .substring(File.getOriginalFilename().lastIndexOf("."));

        InputStream inputStream = null;
        try {
            inputStream = File.getInputStream();
            String url = OssUtils.upload(name, fileType, inputStream);

            return Result.bulid(url);
        } catch (IOException e) {
            throw  new DIYException(-9999,"上传文件失败");
        }


    }

    @Override
    public Result OrganizationWork(HisCreateSchedulingParam createSchedulingParam) {

            //医生排班
            QueryCriteriaStaffParam queryCriteriaStaffParam = CopyBeanUtil.copyBean(createSchedulingParam, QueryCriteriaStaffParam.class);
            QueryStaffParam queryStaffParam = new QueryStaffParam();
            queryStaffParam.setQueryCriteriaStaffParam(queryCriteriaStaffParam);
            //根据员工id 查询员工信息
            Result<QueryStaffDto> queryStaffDtoResult = hisStaffAPI.queryStaff(queryStaffParam);

            PageDTO<StaffDto> staffDtoList = queryStaffDtoResult.getData().getStaffDtoList();
            List<StaffDto> list = staffDtoList.getList();
            StaffDto staffDto = list.get(0);
            BeanUtils.copyProperties(staffDto,createSchedulingParam);

            CreateSchedulingParam createSchedulingParam1 = CopyBeanUtil.copyBean(createSchedulingParam, CreateSchedulingParam.class);
            Result<SchedulingDto> scheduling = hisSchedulingAPI.createScheduling(createSchedulingParam1);
            if (createSchedulingParam.getBedNum()!=0){
                log.info("{}病房"+createSchedulingParam.getBedNum());
                HisBedSetParam hisBedSetParam = new HisBedSetParam();
                hisBedSetParam.setRoomId(createSchedulingParam.getBedNum());
                Result<PageDTO<HisBedDto>> pageDTOResult = BedInformation(hisBedSetParam);
                List<HisBedDto> list1 = pageDTOResult.getData().getList();
                HisBedDto hisBedDto = list1.get(0);
                log.info("{}"+hisBedDto);
                hisBedDto.setNurseId(createSchedulingParam.getStaffId());
                log.info("{}员工id"+createSchedulingParam.getStaffId());
                HisBedParam hisBedParam = CopyBeanUtil.copyBean(hisBedDto, HisBedParam.class);

                Result<HisBedDto> hisBedDtoResult = hisBedApi.updateHisBed(hisBedParam);
                return hisBedDtoResult;
            }
            return scheduling;


    }

    @Override
    public Result DeleteRoster(HisDelSchedulingParam schedulingParam) {
        return null;
    }

    @Override
    public Result WardInformation(PageParam pageParam) {
        Result<PageDTO<HisRoomDto>> allRoom = hisRoomApi.getAllRoom(pageParam);

        return  allRoom ;
    }

    @Override
    public Result CreateWords(HisBedAddParam hisBedAddParam) {
        HisQueryStaffParam hisqueryStaffParam = new HisQueryStaffParam();
        hisqueryStaffParam.getQueryCriteriaStaffParam().setStaffId(hisBedAddParam.getNurseId());
        QueryStaffParam queryStaffParam = CopyBeanUtil.copyBean(hisqueryStaffParam, QueryStaffParam.class);
        Result<QueryStaffDto> queryStaffDtoResult = hisStaffAPI.queryStaff(queryStaffParam);
        PageDTO<StaffDto> staffDtoList = queryStaffDtoResult.getData().getStaffDtoList();
        return Result.bulid(staffDtoList);
    }

    @Override
    public Result NurseInformation() {
        HisQueryStaffParam hisqueryStaffParam = new HisQueryStaffParam();
        HisQueryCriteriaStaffParam hisqueryCriteriaStaffParam = new HisQueryCriteriaStaffParam();
         hisqueryCriteriaStaffParam.setStaffIdentity(1);
        QueryCriteriaStaffParam queryCriteriaStaffParam = CopyBeanUtil.copyBean(hisqueryCriteriaStaffParam, QueryCriteriaStaffParam.class);

        PageParam pageParam = new PageParam();
                pageParam.setPage(1L);
                pageParam.setPageSize(1L);

        QueryStaffParam queryStaffParam = CopyBeanUtil.copyBean(hisqueryStaffParam, QueryStaffParam.class);
           queryStaffParam.setPageParam(pageParam);
           queryStaffParam.setQueryCriteriaStaffParam(queryCriteriaStaffParam);
        System.out.println("====================="+queryStaffParam);
        Result<QueryStaffDto> queryStaffDtoResult = hisStaffAPI.queryStaff(queryStaffParam);
        QueryStaffDto data = queryStaffDtoResult.getData();
        PageDTO<StaffDto> staffDtoList = data.getStaffDtoList();

        return Result.bulid(staffDtoList);
    }

    @Override
    public Result<PageDTO<HisBedDto>>BedInformation(HisBedSetParam hisBedSetParam) {
        HisBedParam hisBedParam = new HisBedParam();
        hisBedParam.setRoomId(hisBedSetParam.getRoomId());
        Result<PageDTO<HisBedDto>> hisBedBy = hisBedApi.getHisBedBy(hisBedParam);

        return hisBedBy;
    }

    @Override
    public Result WorkInformation(HisCreateSchedulingParam hisCreateSchedulingParam) {
        QuerySchedulingParam querySchedulingParam = CopyBeanUtil.copyBean(hisCreateSchedulingParam, QuerySchedulingParam.class);
        Result<QuerySchedulingDto> querySchedulingDtoResult = hisSchedulingAPI.queryScheduling(querySchedulingParam);

        return querySchedulingDtoResult;
    }


}
